package oop.core.view.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.swing.JOptionPane;

import oop.core.RunGame;
import oop.core.control.Controller;
import oop.core.view.ControllerConfigMenu;
import oop.core.view.MainMenu;

/**
 * This class is activated upon pressing the Accept button in the
 * ControllerConfigMenu. Handles the actions associated with
 * reseting the KeyboardInterpreter associated with the Controller
 * with the user-set HashMap.
 * @author Stephanie
 *
 */

public class AcceptControllerConfigListener implements ActionListener {
	
	private Controller controller;
	private ControllerConfigMenu menu;

	public AcceptControllerConfigListener(Controller controller, ControllerConfigMenu menu )
	{
		this.controller = controller;
		this.menu = menu;
	}
	
	/**
	 * This method is fired when the Accept button is pressed in the 
	 * ControllerConfigMenu. First it checks if any of the keys are the same.
	 * If so, it notifies the user and then allows the user to
	 * edit the controls again. If there are no duplicates, then the 
	 * changes are saved and the controller changes its key map
	 * to reflect those changes.
	 */
	
	@Override
	public void actionPerformed(ActionEvent ae) {
		if(menu.isValidConfig())
		{
			controller.setKeyMap(menu.getNewConfig());
			RunGame.changePanel(MainMenu.ID);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Configuration is not valid.");
		}
	}

}
